// pages/sendlose/sendlose.js
const db = wx.cloud.database(); //初始化数据库
import {
  getTimeNow
} from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNow: 0,
    tabList: ['招领', '寻物'],
    a: '',
    title: '',
    imgs: [],
    index2: 0,
    array2: ['招领', '寻物'],
    losetime: '',
    losedidian: '',
    loselianxi: '',
    beizhu: '',
    type: '',
  },
  /* 顶部导航栏 */
  selectTab(e) {
    const {
      id
    } = e.currentTarget.dataset;
    this.setData({
      tabNow: id,
    })
    if (id === 0) {
      this.setData({
        type: '招领',
      })
    } else if (id === 1) {
      this.setData({
        type: '寻物',
      })
    }
  },

  /* 标题 */
  gettitle(e) {
    this.setData({
      title: e.detail.value,
    })
  },
  getlosetime(e) {
    this.setData({
      losetime: e.detail.value,
    })
  },
  getlosedidian(e) {
    this.setData({
      losedidian: e.detail.value,
    })
  },
  getloselianxi(e) {
    this.setData({
      loselianxi: e.detail.value,
    })
  },
  getbeizhu(e) {
    this.setData({
      beizhu: e.detail.value,
    })
  },
  // 上传图片
  upimgs() {
    let that = this
    wx.showActionSheet({
      itemList: ['上传图片'],
      success(res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 3,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
              wx.showLoading({
                title: '上传中...',
              })
              let time = Date.now()
              for (var i = 0; i <= res.tempFilePaths.length; i++) {
                wx.cloud.uploadFile({
                    cloudPath: "loseimage/" + time + i,
                    filePath: res.tempFilePaths[i]
                  })
                  .then(res => {
                    that.setData({
                      imgs: that.data.imgs.concat(res.fileID)
                    })
                    wx.hideLoading()
                    wx.showToast({
                      icon: "none",
                      title: '上传成功',
                    })
                  })
              }
            }
          })
        }
      }
    })
  },
  // 点击预览图片
  look(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: this.data.imgs,
    })
  },


  cc(res) {
    this.setData({
      index2: res.detail.value,
    })
  },

  // 点击发布数据 进数据库
  submit() {
   // const user_key = wx.getStorageSync('key');
    const that = this.data;
    if (!that.title || !that.loselianxi || !that.losetime || !that.imgs || !that.losedidian) {
      wx.showToast({
        icon: 'none',
        title: '您填写的信息不全',
      })
      return;
    } 
    else {
     // console.log(wx.getStorageSync('key'))
      if (wx.getStorageSync('key') != "" && wx.getStorageSync('key') != null) {
        db.collection('lose').add({
          data: {
            // 当前时间
            time: getTimeNow(),
            // 标题
            title: that.title,
            losetime: that.losetime,
            imgs: that.imgs,
            losedidian: that.losedidian,
            loselianxi: that.loselianxi,
            beizhu: that.beizhu,
            // 分类
            // type: that.array2[that.index2],
            type: that.type,

            //用户信息
            userInfo: wx.getStorageSync('key'),
            createTime: db.serverDate()
          },
          success: (res) => {
            wx.showToast({
                title: '发布成功',
              }),
              setTimeout(() => { // 延时跳转
                /*                 wx.navigateTo({
                                  url: '../lose/lose',
                                }) */
                wx.reLaunch({  
                  url: '/pages/lose/lose',
                })
 
              }, 500)
          }
        })
      } else {
        wx.showToast({
          title: '你还没登录噢',
          icon: 'none'
        });
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/my/my',
          })
        }, 500)
      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 过审核
    const that = this
    db.collection("a").get()
      .then(res => {
        console.log("11111111", res.data[0].a)
        that.setData({
          a: res.data[0].a,
        })
      });
      this.setData({
        type: '招领',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})